<?php

namespace Plu\qdh\Services;

use Plu\qdh\Services\ServiceBase;
use Plu\qdh\Services\OrderService;
use Plu\qdh\Services\ThirdService;
use Plu\qdh\Models\App\Order;
use Plu\qdh\Models\App\QrCode;
use Plu\qdh\Models\App\Report;
use Plu\qdh\Models\App\Auth;

class CornService extends ServiceBase {

    private $_orderModel;

    private $_startTime;

    private $_endTime;

    private $_today;

    const SELF_SHOP = [
        'id'     => 0,
        'name'   => '自营',
        'imgUrl' => '',
    ];

    // 每5分钟执行一次订单过期脚本
    public function expireOrders() {
        $order = new Order;
        if ($order->excuteTask()) {
            echo "expire finished".PHP_EOL;
        } else {
            echo "expire FAILED".PHP_EOL;
        }
    }

    // 每3分钟查询第三方审核订单状态， 跟新至数据库
    public function updatePendingOrderStatus() {
        $order = new Order;


    }


    // 每天11点59开始结算脚本
    public function reportDaliyData() {

        $this->_today     = date('Y-m-d', time());

        $this->_startTime = strtotime($this->_today);

        $this->_endTime   = strtotime($this->_today.' +1 day');

        $custs   = $this->getCusts();

        $custs[] = self::SELF_SHOP;

        $custIds = array_column($custs, 'id');

        $this->_orderModel = new Order;

        foreach ($this->getCustOrders($custIds) as $report) {
            echo "save ".$report['custId']." datas ".PHP_EOL;
            $save = $this->saveReportDaliyData($report);
            print_r($report);
            echo " RES = ".$save.PHP_EOL;
        }

        echo "FINISHRD".PHP_EOL;

        exit;

    }

    // 获取分销商下订单
    private function getCustOrders($custIds) {

        foreach ($custIds as $custId) {

            $payOrders     = $this->_orderModel->multDaliyProfitsReport($custId, $this->_startTime, $this->_endTime);

            $refoundOrders = $this->_orderModel->multDalitRefindReport($custId, $this->_startTime, $this->_endTime);

            $response = [
                'custId'  => $custId,
                'pay'     => $payOrders?$payOrders->toArray():[],
                'refound' => $refoundOrders?$refoundOrders->toArray():[]
            ];

            yield $response;
            
        }

    }


    private function saveReportDaliyData($saveData) {

        $custId   = $saveData['custId'];

        $reFounds = $saveData['refound'][0];
        
        $payDatas = $saveData['pay'][0];

        $reportModel = new Report;

        $reportModel->day    = $this->_today;

        $reportModel->custId = $custId;

        $reportModel->totalNum = $payDatas['totalNum']?$payDatas['totalNum']:0;

        $reportModel->totalPrice = $payDatas['totalPrice']?$payDatas['totalPrice']:0;

        $reportModel->spreadPrizeTotal = $payDatas['totalspreadPrize']?$payDatas['totalspreadPrize']:0;

        $reportModel->totalSettlementPrice = $payDatas['totalSettlementPrice']?$payDatas['totalSettlementPrice']:0;
        
        $reportModel->redoundTotalNum = $reFounds['redoundTotalNum']?$reFounds['redoundTotalNum']:0;

        $reportModel->refoundTotalPrice = $reFounds['refoundTotalPrice']?$reFounds['refoundTotalPrice']:0;

        $reportModel->refoundtotalSettlementPrice = $reFounds['refoundtotalSettlementPrice']?$reFounds['refoundtotalSettlementPrice']:0;
       
        return $reportModel->save();

    }

    // 获取分销商
    private function getCusts() {
        $custs = QrCode::getAllItems();
        return $custs;
    }


    public function createAdminAccount() {
        $username = "qdhAdmin";
        $pswd     = "125800Qdh";
        $pswd     = md5($pswd);
        $Auth     = new Auth;
        $Auth->user_name = $username;
        $Auth->password = $pswd;
        $Auth->phone = 0;
        $Auth->create_time = time();
        $Auth->save();
    }




}